package com.etop.jansing.service;

import com.etop.jansing.dao.PermissionDao;
import com.etop.jansing.dao.ResourcePermissionDao;
import com.etop.jansing.entities.Permission;
import com.etop.jansing.entities.ResourcePermission;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jansing on 2015/10/26.
 */
@Service
public class PermissionService extends BaseService{

    @Resource
    private PermissionDao permissionDao;
    @Resource
    private ResourcePermissionDao resourcePermissionDao;

    public Permission getById(long id) {
        log.info("-->获取id=["+id+"]的权限");
        Permission permission = permissionDao.get(id);
        if(permission==null) {
            log.warn("-->id=[" + id + "]的权限不存在");
        }
        return permission;
    }

    public Permission getByNameIgnoreValid(String name) {
        log.info("-->获取name=["+name+"]的权限");
        Permission permission = permissionDao.getByNameIgnoreValid(name);
        if(permission==null) {
            log.warn("-->name=[" + name + "]的权限不存在");
        }
        return permission;
    }

    public void saveOrUpdate(ResourcePermission resoPerm) {
        if (resoPerm.getId() != null && resoPerm.getId() > 0) {
            log.info("-->更新权限");
            resourcePermissionDao.update(resoPerm);
        } else {
            log.info("-->保存权限");
            resourcePermissionDao.save(resoPerm);
        }
    }

    public boolean isUsing(ResourcePermission resoPerm) {
        if (permissionDao.isUsing(resoPerm) > 0) {
            log.info("-->id=[" + resoPerm.getId() + "]的权限正被使用");
            return true;
        }
        log.info("-->id=[" + resoPerm.getId() + "]的权限没有被使用");
        return false;
    }

    public void delete(ResourcePermission resoPerm) {
        if (resoPerm == null) {
            log.info("-->id=["+resoPerm.getId()+"]的权限不存在");
            return;
        }
        log.info("-->删除id=["+resoPerm.getId()+"]的权限");
        resourcePermissionDao.delete(resoPerm);
    }

    public List<Permission> getAllPermissions() {
        log.info("-->获取所有权限");
        return permissionDao.list();
    }

    /**
     * 获取资源对应的权限
     *
     * @param resource
     * @return
     */
    public List<Permission> getByResource(com.etop.jansing.entities.Resource resource) {
        log.info("-->获取id=["+resource.getId()+"]的资源的所有权限");
        if (resource.getId() == null || resource.getId() <= 0) {
            log.info("-->id=["+resource.getId()+"]的资源不存在");
            return new ArrayList<>();
        }
        return permissionDao.getByResourceId(resource.getId());
    }

    /**
     * 获取被角色引用的权限
     *
     * @param resource
     * @return
     */
    public List<Permission> getUsingPermission(com.etop.jansing.entities.Resource resource) {
        log.info("-->获取id=["+resource.getId()+"]的资源的正被使用的所有权限");
        if (resource.getId() == null || resource.getId() <= 0) {
            log.info("-->id=[" + resource.getId() + "]的资源不存在");
            return new ArrayList<>();
        }
        return permissionDao.getUsingPermission(resource.getId());
    }

    public ResourcePermission getResoPermById(long id){
        log.info("-->获取id=["+id+"]的资源权限对");
        return resourcePermissionDao.get(id);
    }

    public ResourcePermission getResoPermByResoPermString(String resoPermString){
        log.info("-->获取resoPermString=["+resoPermString+"]的资源权限对");
        return resourcePermissionDao.getByResoPermString(resoPermString);
    }
}
